<?php
namespace app\common\server;
use x\Xbase;
class Orderxiangmu extends Xbase{
    //分润
    public function award($id){

        $rs   = $this->db->find('order_xiangmu', $id);
        $user = $this->db->find('user',$rs['user_id']);
        $s_tree  = $user['s_tree'];
        $daili   = $this->db->query("select id from user where id in(".trim($s_tree,',').") and level=1 order by s_level desc",[],1);
        $this->db->begin();
        $mysql = [];
        #代理奖
        if($daili){
          $money = bcmul($rs['real_money'],0.4,2);
          if($money>0){
              $d = [
                'user_id' => $daili['id'],
                'money'   => $money,
                'order_xiangmu_id' => $rs['id'],
                'create_time' => time(),
              ];
              $mysql[] = $id = $this->db->insert("award_daili", $d);
              $mysql[] = m('userbill','c')->doit('', $d['user_id'], 100,'award_daili', $id,$money,'money','代理收益');
          }
        }
        #推广奖
        if($user['s_id']){
          $money = bcmul($rs['real_money'],0.3,2);
          if($money>0){
              $d = [
                'user_id' => $user['s_id'],
                'money'   => $money,
                'order_xiangmu_id' => $rs['id'],
                'create_time' => time(),
              ];
              $mysql[] = $id = $this->db->insert("award_tuiguang", $d);
              $mysql[] = m('userbill','c')->doit('', $d['user_id'], 101,'award_tuiguang', $id,$money,'money','推广收益');
          }
        }
        $mysql[] = $this->db->update('order_xiangmu', ['id'=>$rs['id'],'is_count'=>1]);
        $rs = $this->db->end($mysql);
        if (!$rs) {
          return $this->setError('分润失败');
        }
        return true;
    }
}
